package edu.nf.webapp.controller;

import edu.nf.webapp.entity.User;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.nf.web.annotation.Model;
import org.nf.web.annotation.RequestMapping;
import org.nf.web.annotation.RequestParam;
import org.nf.web.servlet.View;
import org.nf.web.view.ForwardView;
import org.nf.web.view.JsonView;
import org.nf.web.view.RedirectView;

import java.util.Date;

/**
 * @Author czl
 * @Date 2024-05-16
 */
public class LoginController {
    @RequestMapping("/login")
    public void login(HttpServletRequest req){
        System.out.println(req);
        System.out.println("login...");
    }

    @RequestMapping("/red")
    public void red( HttpServletResponse response){
        System.out.println(response);
        System.out.println("red");
    }

    @RequestMapping("/add")
    public void add(HttpSession session){
        System.out.println(session);
        System.out.println("add");
    }

    @RequestMapping("/add2")
    public void add2(@RequestParam("name") String name ,
                    @RequestParam("age") int age){

        System.out.println(name + " " + age);
    }

    @RequestMapping("/add3")
    public void add3(@Model User user){
        System.out.println(user.getName() + " " + user.getAge());
    }

    @RequestMapping("/redirect")
    public View redirect(){
        return new RedirectView("/index.html");
    }

    @RequestMapping("/forward")
    public View forward(){
        ForwardView fv = new ForwardView("index.jsp");
        fv.addObject("name","user1");
        return fv;
    }

    @RequestMapping("/testView")
    public String test(){
        return "Hello View";
    }

    @RequestMapping("/json")
    public View toJson(){
        User user = new User();
        user.setName("user1");
        user.setAge(18);
        user.setDate(new Date());
        JsonView view = new JsonView(user);
        view.format("yyyy-MM-dd");
        return view;
    }
}
